/*
New Scotland Yard is an online multiplayer adaptation
of the boardgame "Scotland Yard". Copyright (C) 2011
Massey University Software C Group 3
This program is free software: you can redistribute it
and/or modify it under the terms of the GNU General
Public License as published by the Free Software
Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General
Public License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
package nz.ac.massey.softwarec.group3.actions;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import nz.ac.massey.softwarec.group3.game.Game;
import nz.ac.massey.softwarec.group3.game.Player;
import nz.ac.massey.softwarec.group3.game.map.Map;
import nz.ac.massey.softwarec.group3.session.SessionListener;
import org.xml.sax.SAXException;
/**
* ActionPerformer - Class for the object that takes an action and performs it
* based on the type of actions.
* @version 1.0 Release
* @since 1.0
* @authors Natalie Eustace | Wanting Huang | Paul Smith | Craig Spence
*/
public final class ActionPerformer {
private ActionPerformer() {}
/**
* Takes the action and if the player is ready, it sets the player to ready, if it is start game
* then the game gets started, if it is setmap then it creates a game and gives it a new map and
* creates the session associated with that game. It will also set the map name for the game, and
* the creator for the game if given the proper type.
*
* If joingame is the actionType, it adds the user to the game and and then updates the game session.
*
* The end methods allow the players to move and how the game ends.
* @param action - The Action object to be performed.
*/
public static void performAction(final Action action) {
final String actionType = action.getActionType();
final String value = action.getValue();
Game game = action.getGame();
final HttpSession session = action.getSession();
if ("startgame".equals(actionType)) {
game.setPlaying(true);
game.startGame();
}
if ("setmap".equals(actionType)) {
game = SessionListener.getSessionTracker().getGameTracker().createNewGame((String) session.getAttribute("email"));
game.setMap(new Map(value));
session.setAttribute("game", game);
}
if ("setmapname".equals(actionType)) {
game.getMap().setMapName(value);
}
if ("setgamecreator".equals(actionType)) {
game.setCreatorName(value);
game.tellLobby();
}
if ("joingame".equals(actionType)) {
game = SessionListener.getSessionTracker().getGameTracker().getGameMadeByCreator(value);
game.addUserToGame((String) session.getAttribute("email"));
session.setAttribute("game", game);
}
if("takeTurn".equals(actionType)) {
try {
game.playerTakesTurn(value);
} catch (ParserConfigurationException ex) {
System.err.print(ex);
} catch (SAXException ex) {
System.err.print(ex);
} catch (IOException ex) {
System.err.print(ex);
}
}
if ("startDoubleMove".equals(actionType)) {
game.startDoubleTurn();
}
if ("cancelDoubleMove".equals(actionType)) {
game.cancelDoubleTurn();
}
if("cannotMove".equals(actionType)) {
game.getPlayers().get(Integer.parseInt(value)).setCanMove(false);
}
if("checkGameEnd".equals(actionType)) {
game.checkPlayersLose();
}
if("leaveGame".equals(actionType)) {
if (game != null) {
for (Player player : game.getPlayers()) {
if (player.getPlayerEmail().equals(value)) {
player.leaveGame();
if (game.getWhosTurn() == game.getPlayers().indexOf(player)) {
game.determineWhosTurnItIsNext();
}
}
}
session.setAttribute("game", null);
if (!game.isFinished()) {
game.checkEnd();
}
}
}
}
}